package com.icat.house.web;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;

public class AppConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		// 自动扫描 建议用注解
		me.add(new AutoBindRoutes().autoScan(false));
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 站点数据库配置
		C3p0Plugin dsSite = new C3p0Plugin(PropKit.get("db.site.url"), PropKit.get("db.site.user"),
				PropKit.get("db.site.password"), PropKit.get("db.site.driver"));
		me.add(dsSite);
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(dsSite, SimpleNameStyles.LOWER_UNDERLINE).autoScan(false);
		me.add(atbp);
		ActiveRecordPlugin arpSite = new ActiveRecordPlugin("site", dsSite);
		arpSite.setDialect(new MysqlDialect());
		arpSite.setShowSql(PropKit.getBoolean("devMode", false));
		me.add(arpSite);
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx"));
		me.add(new DruidStatViewHandler("/druid"));
		me.add(new UrlSkipHandler(".+\\.\\w{1,4}", false));
	}

	/**
	 * 资源初始化
	 */
	public void afterJFinalStart() {
	}

	/**
	 * 资源清理
	 */
	public void beforeJFinalStop() {

	}

	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
